package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Menu;
import com.qf.entity.Role;
import com.qf.enums.Page;
import com.qf.mapper.RoleMapper;
import com.qf.mapper.UserMapper;
import com.qf.service.RoleService;
import com.qf.utils.MenuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 2019/12/11
 * 郭程鹏
 * springboot_oa_five
 * 面向代码面向君,不负时光不负卿
 */

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserMapper userMapper;

    //模糊查询
    @Override
    public PageInfo<Role> findAllRoles(String roleName, Integer page) {


        PageHelper.startPage(page, Page.pageSize.getSize());

        List<Role> roles = roleMapper.findRoles(roleName);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);

        return rolePageInfo;

    }

    //导出excle
    @Override
    public List<Role> getExcle(List<Integer> list) {
        List<Role> excle = roleMapper.getExcle(list);
        return excle;
    }

    //批量删除
    @Transactional
    @Override
    public void deleteAll(List<Integer> list) {
        roleMapper.deleteAll(list);
    }

    //删除单条数据
    @Transactional
    @Override
    public void deleteOne(Integer roleId) {
        roleMapper.deleteOne(roleId);
    }

    //判断角色是否可删
    @Override
        public List<String> checkCanDel(Integer roleId) {
            List<String> strings = roleMapper.checkCanDel(roleId);
            return strings;
    }

    //统计不可删除的角色
    @Override
    public List<Integer> checkDelDo(List<Integer> roleId) {
        ArrayList<Integer> arrayList = new ArrayList<>();

        for (Integer id : roleId) {
            List<String> list = roleMapper.checkCanDel(id);
            if (list.size()>0){
               arrayList.add(id);
            }
        }
        return arrayList;
    }

    //禁用
    @Transactional
    @Override
    public void updateOne(Integer roleId) {
        roleMapper.updateOne(roleId);
    }

    //启用
    @Transactional
    @Override
    public void activeOne(Integer roleId) {
     roleMapper.activeOne(roleId);
    }

    //查询对应菜单
    @Override
    public List<Menu> findMenu() {
        List<Menu> menu = roleMapper.findMenu();
        List<Menu> firstMenu = MenuUtil.firstMenu(menu);
        return firstMenu;
    }

    //异步校验用户名
    @Override
    public Integer ajaxRoleName(String roleName) {
        Integer integer = roleMapper.ajaxRoleName(roleName);
        return integer;
    }

    //添加角色和中间表
    @Transactional
    @Override
    public Integer addRoleAndMiddle(Role role, List<Integer> menuId) {
        Integer addRole= roleMapper.addRole(role);
        Integer sum=addRole;
        for (Integer mId : menuId) {
            Integer roleMenu = roleMapper.addRoleMenu(role.getRoleId(), mId);
            sum=sum+roleMenu;
        }

        return sum;
    }

    //根据角色编号查询角色
    @Override
    public Role findRoleById(Integer roleId) {
        Role role = roleMapper.findRoleById(roleId);
        return role;
    }

    //删除中间表
    @Override
    @Transactional
    public void delMenuIdByUserIdInMiddle(Integer roleId) {
        roleMapper.delMenuIdByUserIdInMiddle(roleId);
    }

    //修改角色信息 删除中间表信息 修改角色信息  添加中间表
    @Transactional
    @Override
    public Integer updateRoleAndMiddle(Role role, List<Integer> menuId) {
        Integer sum=0;
        delMenuIdByUserIdInMiddle(role.getRoleId());
        roleMapper.updateRole(role);

        for (Integer mId : menuId) {
            Integer roleMenu = roleMapper.addRoleMenu(role.getRoleId(), mId);
            sum=sum+roleMenu;
        }
        return sum;
    }

    //根据角色id查询角色菜单
    @Override
    public List<Menu> findMenuByRoelId(Integer roleId) {

        List<Menu> menuByRoleId = userMapper.findMenuByRoleId(roleId);
        List<Menu> firstMenu = MenuUtil.firstMenu(menuByRoleId);
        return firstMenu;
    }

    //根据角色角色编号查询角色名称
    @Override
    public List<String> findRoleNameByUserId(List<Integer> roleId) {
        ArrayList<String> list = new ArrayList<>();
        for (Integer integer : roleId) {
            Role roleById = findRoleById(integer);
            list.add(roleById.getRoleName());
        }
        return list;
    }


}
